package com.lysj.admin.master.bussiness.company.invoice.service;

import com.lysj.admin.master.bussiness.admin.domain.ServiceProvider;
import com.lysj.admin.master.bussiness.admin.service.ServiceProviderService;
import com.lysj.admin.master.bussiness.company.invoice.domain.InvoiceApply;
import com.lysj.admin.master.bussiness.company.invoice.domain.InvoiceApplyItem;
import com.lysj.admin.master.bussiness.company.invoice.domain.InvoiceInfo;
import com.lysj.admin.master.bussiness.company.invoice.domain.InvoiceRecipient;
import com.lysj.admin.master.bussiness.company.invoice.dto.InvoiceApplyItemSubmitDto;
import com.lysj.admin.master.bussiness.company.invoice.mapper.InvoiceApplyItemMapper;
import com.lysj.admin.master.common.exception.BaseException;
import com.lysj.admin.master.common.spring.mpbase.MyService;
import com.lysj.admin.master.common.web.Resp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * @author Created by drj on 2020-01-09 10:47
 * @description
 */
@Slf4j
@Service
@Transactional
public class InvoiceApplyItemService extends MyService<InvoiceApplyItemMapper, InvoiceApplyItem> {

    @Resource
    private InvoiceApplyService invoiceApplyService;
    @Resource
    private ServiceProviderService serviceProviderService;
    @Resource
    private InvoiceInfoService invoiceInfoService;
    @Resource
    private InvoiceRecipientService invoiceRecipientService;


    public String audit(String id, Integer invoiceStatus) {

        InvoiceApplyItem invoiceApplyItem = this.getById(id);
        if (!InvoiceApplyItem.InvoiceStatus.AUDITING.getCode().equals(invoiceApplyItem.getInvoiceStatus())) {
            throw new BaseException(Resp.ResStatus.PARAM_ERROR, "开票记录非待审核状态,请勿重复操作");
        }
        invoiceApplyItem.setInvoiceStatus(invoiceStatus);
        this.saveOrUpdate(invoiceApplyItem);
        //如果选择的是驳回,那么退回本次提交的开票金额
        if (InvoiceApplyItem.InvoiceStatus.REJECTED.getCode().equals(invoiceStatus)) {
            InvoiceApply invoiceApply = invoiceApplyService.getById(invoiceApplyItem.getInvoiceApplyId());
            invoiceApply.setLeftMoney(invoiceApply.getLeftMoney().add(invoiceApplyItem.getInvoicePrice()));
            invoiceApplyService.saveOrUpdate(invoiceApply);
        }
        return "操作成功";
    }


    public String submit(InvoiceApplyItemSubmitDto model) {

        //开票金额判断
        InvoiceApply invoiceApply = invoiceApplyService.getById(model.getInvoiceApplyId());
        if (model.getInvoicePrice().compareTo(invoiceApply.getLeftMoney()) >= 1) {
            throw new BaseException("当前最高开票金额为: " + invoiceApply.getLeftMoney() + "元");
        }
        //扣除开票金额
        invoiceApply.setLeftMoney(invoiceApply.getLeftMoney().subtract(model.getInvoicePrice()));
        invoiceApplyService.saveOrUpdate(invoiceApply);


        InvoiceApplyItem invoiceApplyItem = new InvoiceApplyItem();

        //获取服务商信息
        ServiceProvider serviceProvider = serviceProviderService.getById(invoiceApply.getServiceProviderId());
        invoiceApplyItem.setSerName(serviceProvider.getName());
        invoiceApplyItem.setSerTaxNumber(serviceProvider.getTaxNumber());
        invoiceApplyItem.setSerAddress(serviceProvider.getAddress());
        invoiceApplyItem.setSerPhone(serviceProvider.getPhone());
        invoiceApplyItem.setSerBankName(serviceProvider.getBankName());
        invoiceApplyItem.setSerAccountName(serviceProvider.getAccountName());

        //获取购买方信息
        InvoiceInfo invoiceInfo = invoiceInfoService.findByCompanyId(invoiceApply.getCompanyId());
        invoiceApplyItem.setInvoiceEnterprise(invoiceInfo.getEnterprise());
        invoiceApplyItem.setInvoiceTaxNumber(invoiceInfo.getTaxNumber());
        invoiceApplyItem.setInvoiceAddress(invoiceInfo.getAddress());
        invoiceApplyItem.setInvoicePhone(invoiceInfo.getPhone());
        invoiceApplyItem.setInvoiceBank(invoiceInfo.getBank());
        invoiceApplyItem.setInvoiceAccount(invoiceInfo.getAccount());

        //获取收件人信息
        InvoiceRecipient invoiceRecipient = invoiceRecipientService.getById(model.getInvoiceRecipientId());
        invoiceApplyItem.setRecAddress(invoiceRecipient.getAddress());
        invoiceApplyItem.setRecPhone(invoiceRecipient.getPhone());
        invoiceApplyItem.setRecName(invoiceRecipient.getName());

        //接收子项参数
        invoiceApplyItem.setInvoiceApplyId(model.getInvoiceApplyId());
        invoiceApplyItem.setInvoicePrice(model.getInvoicePrice());
        invoiceApplyItem.setGoodsName(model.getGoodsName());
        invoiceApplyItem.setInvoiceStatus(InvoiceApplyItem.InvoiceStatus.AUDITING.getCode());
        this.save(invoiceApplyItem);
        return "操作成功";
    }
}
